import {DBManager, Storage} from "foundation";


class WXStorage implements Storage{
  async get(key: string): Promise<[string, Error | null]> {
    const ret = await Taro.getStorage<string>({key});
    if (ret.errMsg) {
      return ["", new Error(ret.errMsg)];
    }

    return [ret.data, null];
  }

  async remove(key: string): Promise<Error | null> {
     const ret = await Taro.removeStorage({key});
     return ret.errMsg?new Error(ret.errMsg):null;
  }

  async set(key: string, value: string): Promise<Error | null> {
    const ret = await Taro.setStorage({key, data:value});
    return ret.errMsg?new Error(ret.errMsg):null;
  }
}

export class WXDBManager extends DBManager {
  protected newSessionStorage():Storage {
    return new WXStorage();
  }

  protected newLocalStorage():Storage {
    return new WXStorage();
  }
}
